<?php
declare(strict_types=1);
namespace App\System\Controller;

use Haozing\FastCore\Abstract\AbstractController;
use Haozing\FastCore\Constants\ErrorCode;
use Haozing\FastCore\Exception\CommonException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\System\Logic\HeiUserLogic;
use Psr\Http\Message\ResponseInterface;

#[Controller]
class HeiUserController extends AbstractController
{
    #[Inject]
    protected HeiUserLogic $heiUserLogic;

    /**
     * 新增
     * @return array
     */
    #[RequestMapping(path: '/system/user/add',methods: 'post')]
    public function add():ResponseInterface
    {
        // 验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'username' => 'required',
                'password' => 'required',

            ],
            [
                'username.required' => '用户名不能为空',
                'password.required' => '密码不能为空',

            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiUserLogic->add($params);

        return $this->success($data);
    }
    /**
     * 删除
     * @return array
     */
    #[RequestMapping(path: '/system/heiUser/delete',methods: 'post')]
    public function delete(): array
    {
        // 验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'id' =>'required',
            ],
            [
                'id.required' => 'id不能为空',
            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiUserLogic->delete($params);

        return $data;
    }

    /**
     * 更新
     * @return array
     */
    #[RequestMapping(path: '/system/heiUser/update',methods: 'post')]
    public function update(): array
    {
        // 验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'id' =>'required',
            ],
            [
                'id.required' => 'id不能为空',
            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiUserLogic->update($params);

        return [];
    }

    /**
     * 列表查询
     * @return array
     */
    #[RequestMapping(path: '/system/heiUser/list',methods: 'post')]
    public function list(): array
    {
        // 验证参数
        $params = $this->request->all();
        // 执行业务逻辑
        $data = $this->heiUserLogic->list($params);
        return $data;

    }
    /**
     * 列表查询select
     * @return array
     */
    #[RequestMapping(path: '/system/heiUser/select',methods: 'post')]
    public function select(): array
    {
        // 验证参数
        $params = $this->request->all();
        // 执行业务逻辑
        $data = $this->heiUserLogic->select($params);
        return $data;

    }
    /**
     * 详情
     * @return array
     */
    #[RequestMapping(path: '/system/heiUser/detail',methods: 'get')]
    public function detail(): array
    {
        //验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'id' =>'required',
            ],
            [
                'id.required' => 'id不能为空',
            ]
        );

        if ($validator->fails()){
            throw new CommonException(ErrorCode::INVALID_PARAM,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiUserLogic->detail($params);
        return $data;
    }

}